package com.privatbank.servlets;


/**
 * Created by Slava on 12.07.2014.
 */

import com.privatbank.constants.Constants;
import com.privatbank.user.User;
import com.privatbank.userdao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class UserSaveServlet extends HttpServlet {

    private UserDao dao = null;

    @Override
    public void init() {
        dao = new UserDao(Constants.LOGIN, Constants.PASSWORD, Constants.URL);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name = req.getParameter("name");

        User log = new User();
        log.setName(name);
        try {
            dao.saveUsers(log);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<t1>" + "User successfully inserted. To view the User list follow the link /show " + "</t1>");
        out.println("</body>");
        out.println("</html>");
    }

    private void response(HttpServletResponse resp, String msg)
            throws IOException {
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<t1>" + msg + "</t1>");
        out.println("</body>");
        out.println("</html>");
    }
}
